package GUIHelper;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ControlLayer.RoomTypeCtr;

import GUILayer.RoomTypeModelGUI;
import ModelLayer.KeyWord;
import ModelLayer.RoomType;

//for showing room type GUI
public class RoomTypeInfoPanel2 extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private RoomTypeModelGUI roomTypeModelGUI;
	private JLabel lblname;
	private JLabel lblprice;
	private JLabel lblsize;
	private JLabel lblid;

	private JTextField textField_price;
	private JTextField textField_size;
	private JTextField textField_id;

	private JButton btnShow;

	private JButton btnSave;
	private JButton btnClear;
	private JButton btnDelete;

	private JPanel infoPanel;
	private JPanel btnPanel;

	private JComboBox<String> roomTypes;
	private String[] listStrings = { KeyWord.SingleRoom.toString(),
			KeyWord.DoubleRoom.toString(), KeyWord.FamilyRoom.toString() };

	private RoomTypeCtr rtCtr;

	public RoomTypeInfoPanel2(RoomTypeModelGUI panel) {
		rtCtr = new RoomTypeCtr();
		this.roomTypeModelGUI = panel;
		initialize();

	}

	public void initialize() {

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel spacePanel = new JPanel();
		JLabel lblSpace = new JLabel("  ");
		spacePanel.add(lblSpace);
		this.add(spacePanel);

		infoPanel = new JPanel();
		btnPanel = new JPanel();
		this.add(infoPanel);
		this.add(btnPanel);

		btnShow = new JButton("Show");

		btnSave = new JButton("Save");
		btnClear = new JButton("Clear");
		btnDelete = new JButton("Delete");
		btnPanel.setLayout(new FlowLayout());
		btnPanel.add(btnShow);

		btnPanel.add(btnSave);
		btnPanel.add(btnDelete);
		btnPanel.add(btnClear);

		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setRoomTypeInfo();
			}
		});

		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearInfo();
			}
		});

		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateRoomTypeInfo();
			}
		});

		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (textField_id.getText().isEmpty()) {
					JOptionPane
							.showMessageDialog(
									null,
									"Please show the message of the current room type, before delete it",
									"Error", JOptionPane.ERROR_MESSAGE);
				} else {
					deleteRoomType();
				}
			}
		});

		lblid = new JLabel("Id: ");
		lblname = new JLabel("Name: ");
		lblprice = new JLabel("Price Per Night: ");
		lblsize = new JLabel("Size: ");
		textField_price = new JTextField();
		textField_size = new JTextField();
		textField_id = new JTextField();

		textField_price.setColumns(33);
		textField_size.setColumns(33);
		textField_id.setColumns(33);

		roomTypes = new JComboBox<String>(listStrings);
		roomTypes.setSelectedIndex(2);
		roomTypes.addActionListener(this);

		// set info about room type
		setRoomTypeInfo();

		GroupLayout gl = new GroupLayout(infoPanel);
		infoPanel.setLayout(gl);
		infoPanel.setBorder(BorderFactory.createTitledBorder("RoomType Info"));
		gl.setAutoCreateGaps(true);
		gl.setAutoCreateContainerGaps(true);

		gl.setHorizontalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblid).addComponent(lblname)
								.addComponent(lblprice).addComponent(lblsize))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(textField_id)
								.addComponent(roomTypes)
								.addComponent(textField_price)
								.addComponent(textField_size)));
		gl.setVerticalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblid).addComponent(textField_id))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblname).addComponent(roomTypes))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblprice)
								.addComponent(textField_price))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblsize)
								.addComponent(textField_size)));
	}

	public void deleteRoomType() {
		int reply = JOptionPane
				.showConfirmDialog(
						null,
						"All the information and rooms will be deleted with the room type. \nAre you sure you want to delete it?",
						"Delete Room Type", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE);
		if (reply == JOptionPane.YES_OPTION) {
			try {
				rtCtr.deleteRoomType(Integer.parseInt(textField_id.getText()));
				JOptionPane.showMessageDialog(null, "The room type is deleted",
						"Delete Room Type", JOptionPane.INFORMATION_MESSAGE);
				roomTypeModelGUI.getRtGUI().initialize();
			} catch (Exception e) {
				JOptionPane
						.showMessageDialog(
								null,
								"Failed to delete room type. \nThere may be other objects reference to the room type or its rooms.",
								"Error", JOptionPane.ERROR_MESSAGE);
			}

		}
	}

	public void updateRoomTypeInfo() {
		try {
			rtCtr.updateRoomType(Integer.parseInt(textField_id.getText()),
					listStrings[roomTypes.getSelectedIndex()],
					Double.parseDouble(textField_price.getText()),
					Integer.parseInt(textField_id.getText()));
			// reset room type for roomTypeModelGUI
			RoomType newRt = new RoomType();
			newRt.setId(Integer.parseInt(textField_id.getText()));
			newRt.setName(KeyWord.valueOf(listStrings[roomTypes
					.getSelectedIndex()]));
			newRt.setPricePerNight(Double.parseDouble(textField_price.getText()));
			newRt.setSize(Integer.parseInt(textField_id.getText()));
			roomTypeModelGUI.setRt(newRt);
			roomTypeModelGUI.initialize();
			JOptionPane.showMessageDialog(null, "The room type is updated.",
					"Update Room Type", JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Please input correct value",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void clearInfo() {
		textField_price.setText("");
		textField_size.setText("");
		textField_id.setText("");
		roomTypes.setSelectedIndex(0);
	}

	public void setRoomTypeInfo() {
		textField_price.setText(Double.toString(roomTypeModelGUI.getRt()
				.getPricePerNight()));
		textField_size.setText(Integer.toString(roomTypeModelGUI.getRt()
				.getSize()));
		textField_id
				.setText(Integer.toString(roomTypeModelGUI.getRt().getId()));
		setRoomTypes(roomTypeModelGUI.getRt().getName());
	}

	public void setRoomTypes(KeyWord rt) {
		for (int i = 0; i < listStrings.length; i++) {
			if (rt.toString().equals(listStrings[i]))
				roomTypes.setSelectedIndex(i);
		}
	}

	public void disposeParent() {
		((JFrame) this.getParent().getParent().getParent().getParent())
				.dispose();
	}

	public void createRoomType() {
		String roomType = listStrings[roomTypes.getSelectedIndex()];
		try {
			rtCtr.insertRoomType(roomType,
					Double.parseDouble(textField_price.getText()),
					Integer.parseInt(textField_size.getText()));
			disposeParent();
			JOptionPane.showMessageDialog(null, "The room type is created.",
					"Create Room Type", JOptionPane.INFORMATION_MESSAGE);

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Please input correct value",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// JComboBox cb = (JComboBox) e.getSource();
		// String list = (String) cb.getSelectedItem();

	}
}
